package com.cashg.dao;

import java.io.IOException;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class CashGContentProvider extends RESTfulContentProvider {

	private static final String CASHG_IMAGE_FILE_CACHE = "cashg_image_file_cache";

	public static UriMatcher sUriMatcher;

	public static final int MERCHANT = 1;

	private static final String QUERY_URI = "http://cashg.herokuapp.com/";

	private DatabaseHelper mOpenHelper;
	private SQLiteDatabase mDb;

	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(CashGEntities.AUTHORITY,
				CashGEntities.Merchant.MERCHANT, MERCHANT);

	}

	@Override
	public int delete(Uri arg0, String arg1, String[] arg2) {
		// TODO Auto-generated method stub
		return 0;
	}

	// Not returning MIME type of content now
	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {

		SQLiteDatabase db = getDatabase();
		return insert(uri, values, db);

	}

	@Override
	public boolean onCreate() {
		mOpenHelper = new DatabaseHelper(getContext(),
				DatabaseHelper.DATABASE_NAME, null);
		mDb = mOpenHelper.getWritableDatabase();

		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String where,
			String[] whereArgs, String sortOrder) {
		Cursor queryCursor;

		int match = sUriMatcher.match(uri);
		switch (match) {

		case MERCHANT:
			// First prepare the query to hit cashG API
			String queryText = "member/api/merchant/profile/list/";

			// return whatever we have on DB

			queryCursor = mDb.query(CashGEntities.Merchant.MERCHANT_TABLE_NAME,
					projection, null, whereArgs, null, null, sortOrder);

			// make the cursor observe the requested query
			queryCursor.setNotificationUri(getContext().getContentResolver(),
					uri);

			// Make the actual rest call

			asyncQueryRequest(queryText, QUERY_URI + encode(queryText), uri);
			return queryCursor;

		}
		return null;
	}

	@Override
	public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values, SQLiteDatabase db) {
		int match = sUriMatcher.match(uri);
		switch (match) {
		case MERCHANT:
            long time = System.currentTimeMillis();
            values.put(CashGEntities.Merchant.TIMESTAMP_COLUMN, time);
			long rowId = db.insert(CashGEntities.Merchant.MERCHANT_TABLE_NAME,
					CashGEntities.Merchant.MERCHANT, values);
			return ContentUris.withAppendedId(
					CashGEntities.Merchant.MERCHANT_URI, rowId);
		}
		return null;
	}

	@Override
	public SQLiteDatabase getDatabase() {
		return mDb;
	}

	@Override
	protected ResponseHandler newResponseHandler(String requestTag) {
		// TODO Auto-generated method stub
		try {
			return new CashGResponseHandler(this, requestTag);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
